package com.zgw.his.design.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.internal.resources.Resource;
import org.eclipse.core.resources.IResource;
import org.eclipse.wb.internal.core.utils.IOUtils2;

import com.zgw.his.design.common.LocalResourceHelp;

public class Template extends MedicalResource {
	private Folder folder;
	private String name;
	private String content;
	private String number;// 编号，标识
	private String path;// 其于工作空间的非绝对路径
	private File file;
	private int state = STATE_NOMAL;
	private String version;

	public Template(Folder folder, String name) {
		this.folder = folder;
		this.name = name;
		if (folder != null)
			this.folder.addTemplate(this);
	}

	public Folder getFolder() {
		return folder;
	}

	public String getName() {
		return name;
	}

	public String getContent() {
		if (content == null || content.equals("")) {
			InputStream templateStream = null;
			try {
				templateStream = new FileInputStream(file);
				content = IOUtils2.readString(templateStream);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (templateStream != null)
					try {
						templateStream.close();
					} catch (Exception e) {
					}
			}
			return content;
		}
		return content;
	}

	public String getNumber() {
		return number;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	@Override
	public Resource getResource() {
		if (path != null)
			return LocalResourceHelp.getResource(path, IResource.FILE);
		return null;
	}
	
	
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 获得默认编号
	 * */
	public String getDefaultNumber() {
		return path.replace('/', '.').substring(1);
	}
}
